package edu.cuit.campus.admin.service.bean;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.cuit.campus.admin.service.JobCategoryService;
import edu.cuit.campus.entity.po.JobCategory;
import edu.cuit.campus.entity.vo.JobCategoryView;
import edu.cuit.campus.repository.common.MybatisHelper;
import edu.cuit.campus.repository.mapper.JobCategoryMapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/9 14:00
 */
public class JobCategoryServiceBean implements JobCategoryService {
    @Override
    public PageInfo<JobCategoryView> page(int pageIndex, int pageSize) {
        return page(pageIndex, pageSize, null);
    }

    @Override
    public PageInfo<JobCategoryView> page(int pageIndex, int pageSize, String cateGoryName) {
        PageHelper.startPage(pageIndex, pageSize);
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        var result = mapper.limit(cateGoryName);
        MybatisHelper.close();
        var tree = buildTree(result);
        return new PageInfo<>(tree);
    }

    @Override
    public JobCategory findById(Integer id) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        var category = mapper.findById(id);
        MybatisHelper.close();
        return category;
    }

    @Override
    public void save(JobCategory category) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        mapper.insert(category);
        session.commit();
        MybatisHelper.close();
    }

    @Override
    public boolean removeById(Integer id) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        var result = mapper.deleteById(id);
        session.commit();
        MybatisHelper.close();
        return result == 1;
    }

    @Override
    public List<JobCategory> getParentCategories() {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        var result = mapper.getParentCategories();
        MybatisHelper.close();
        return result;
    }

    @Override
    public void edit(JobCategory category) {
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(JobCategoryMapper.class);
        mapper.updateById(category);
        session.commit();
        MybatisHelper.close();
    }

    private List<JobCategoryView> buildTree(List<JobCategory> categories){
        if(categories == null || categories.isEmpty())
            return Collections.emptyList();

        //树形结构
        List<JobCategoryView> tree = new ArrayList<>();

        var mapper = edu.cuit.campus.entity.mapper.JobCategoryMapper.INSTANCE;

        Map<Integer, JobCategoryView> nodeMap = categories.stream().collect(Collectors.toMap(JobCategory::getId, node-> mapper.jobCategory2JobCategoryView(node)));

        categories.stream().forEach(e -> {
            if(e.getParentId() == null){ //顶层元素
                tree.add(nodeMap.get(e.getId()));
            } else {
                var parent = nodeMap.get(e.getParentId());
                if(parent == null) { //单独查询加载
                    parent = mapper.jobCategory2JobCategoryView(findById(e.getParentId()));
                    tree.add(parent);
                }parent.getChildren().add(nodeMap.get(e.getId()));
            }
        });
        return tree;
    }
}
